<div>
    <h3>绑定第三方账号</h3>
    <%- include("./third-party-login-button.html"); %>
        <div id="withdraw-container" style="display: none;">
            <h3>取消第三方账号绑定</h3>
            <div id="withdraw-buttons"></div>
        </div>
</div>
<script>
    function thirdPartyWithdraw(platform) {
        postRequest("/third-party/withdrawThirdPartyBings", { token: localStorageUtils.getToken(), platform: platform })
            .then(function (response) {
                var axiosData = response.data;
                var status = axiosData.status;
                var data = axiosData.data;
                if (status === "success") {
                    console.log(data);
                    if (data == "success") {
                        swal("取消绑定成功！").then(function () {
                            location.reload();
                        });
                    } else {
                        swal("出错啦，刷新页面重新试试吧");
                    }
                } else {
                    swal(`出错啦！${data.errMsg} (错误码: ${data.errCode})`).then(function () {
                        location.reload();
                    });
                }
            }).catch(function (error) {
                console.log(error);
                swal("无法连接到服务器，请检查网络连接！");
            });
    }
    if (localStorageUtils.getLoginStatus() != null) {
        getRequest("/third-party/getBindingStatus", { token: localStorageUtils.getToken() })
            .then(function (response) {
                var axiosData = response.data;
                var status = axiosData.status;
                var data = axiosData.data;
                if (status === "success") {
                    console.log(data);
                    if (data) {
                        $("#withdraw-container").show();
                        data.forEach(platformName => {
                            // 隐藏绑定按钮
                            var btn = $(`#btn_${platformName.toLowerCase()}`);
                            var platformChineseName = btn.html();
                            btn.attr("disabled", true);
                            btn.html(platformChineseName + "（已绑定）");

                            // 添加取消绑定按钮
                            var withdrawBtn = $(`<button class="btn-third-party" id="btn_${platformName.toLowerCase()}_withdraw" onclick="thirdPartyWithdraw('${platformName}')">取消绑定${platformChineseName}</button>`);
                            $("#withdraw-buttons").append(withdrawBtn);
                            $("#withdraw-buttons").append(" ");
                        });
                    }
                } else {
                    $(".btn-third-party").hide();
                    swal(`出错啦！${data.errMsg} (错误码: ${data.errCode})`);
                }
            }).catch(function (error) {
                console.log(error);
                swal("无法连接到服务器，请检查网络连接！");
            });
    }
</script>